import Vue from "vue";
import VueRouter from "vue-router";
// import Cookies from "js-cookie"

import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
import notFound from "../views/404.vue";
import User from '@/views/SysMng/User'
import Dept from '@/views/SysMng/Dept'
import Role from '@/views/SysMng/Role'
import Menu from '@/views/SysMng/Menu'
import Intro from '@/views/Intro'
import Log from '@/views/SysMng/Log'
import TestCom from '@/views/TestFile/Home.vue'


Vue.use(VueRouter);

const routes = [{
    path: "/",
    name: "Home",
    component: Home,
    children: [{
        path: '',
        component: Intro,
        name: '系统介绍'
      },
      {
        path: '/user',
        component: User,
        name: '用户管理'
      },
      {
        path: '/dept',
        component: Dept,
        name: '机构管理'
      },
      {
        path: '/role',
        component: Role,
        name: '角色管理'
      },
      {
        path: '/menu',
        component: Menu,
        name: '菜单管理'
      },
      {
        path: '/log',
        component: Log,
        name: '日志管理'
      }
    ]
  },
  {
    path: "/TestCom",
    name: "TestCom",
    component: TestCom
  },
  {
    path: "/Login",
    name: "Login",
    component: Login
  },
  {
    path: "/404",
    name: "notFound",
    component: notFound
  }
];

const router = new VueRouter({
  routes
});

// 添加路由守卫  全局前置守卫 根据公户登录会话记录 路由到主页或者登录页面
router.beforeEach((to, from, next) => {
  // 在页面登录成功之后 会把用户信息保存在 会话
  // let token = Cookies.get('token')
  let user = sessionStorage.getItem('user')

  // 测试环境
  if (to.path == '/login') {
    // 如果访问的是登录页面 如果用户信息存在 代表已经登录过，跳转到主页
    if (user) {
      next({
        path: "/"
      })
    } else {
      next()
    }
  } else {
    // 如果访问的不是登录页面 如果用户信息不存在 代表未登录，跳转到登录页面
    if (!user) {
      next({
        path: "/login"
      })
    } else {
      next()
    }
  }
})

export default router;